//
//  BettingDetailsViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/5/13.
//  Copyright © 2016年 刘成. All rights reserved.
//
#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "BettingDetailsViewController.h"
#import "BettingDetailsTableViewCell.h"
#import "BettingRecordModel.h"
#import "G_P_Object.h"
#import "ProcurementHallModel.h"
#import "AFNetworking.h"
@interface BettingDetailsViewController ()

@end

@implementation BettingDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"方案详情";
    self.view.backgroundColor = [UIColor redColor];
    
    [self obtainData1];
    self.bettingDetailstitles1 = @[@"当前状态：",@"订单编号：",@"投注期号：",@"购买金额：",@"中奖金额：",@"购买盈亏：",@"购买倍数：",@"投注注数：",@"中奖注数：",@"购买返点：",@"返点金额：",@"奖金模式：",@"开奖号码：",@"投注号码：",@"投注时间："];
    NSString *state;
    if ([self.bettingRecordModel.B_L_State isEqualToString:@"1"]) {
        state = @"未开奖";
    }
    if ([self.bettingRecordModel.B_L_State isEqualToString:@"2"]) {
        state = @"已中奖";
    }
    if ([self.bettingRecordModel.B_L_State isEqualToString:@"3"]) {
        state = @"未中奖";
    }
    if ([self.bettingRecordModel.B_L_State isEqualToString:@"4"]) {
        state = @"派奖中";
    }
    if ([self.bettingRecordModel.B_L_State isEqualToString:@"5"]) {
        state = @"已撤单";
    }
    if ([self.bettingRecordModel.B_L_State isEqualToString:@"6"]) {
        state = @"完成";
    }
    NSString *win = [NSString stringWithFormat:@"%f",[self.bettingRecordModel.B_L_WinMoney floatValue]-[self.bettingRecordModel.B_L_Money floatValue]];
    self.bettingDetailstitles2 = [NSMutableArray arrayWithArray:@[state,self.bettingRecordModel.B_L_Order,self.bettingRecordModel.B_L_Issue,self.bettingRecordModel.B_L_Money,self.bettingRecordModel.B_L_WinMoney,win,self.bettingRecordModel.B_L_Multiple,self.bettingRecordModel.B_L_BuyNum,self.bettingRecordModel.B_L_WinNum,self.bettingRecordModel.B_L_Rebate,self.bettingRecordModel.B_L_ReturnMoney,self.bettingRecordModel.B_L_BonusMode,@"",@"",self.bettingRecordModel.B_L_AddTime]];


    BettingDetailstableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT-64) style:UITableViewStylePlain];
    BettingDetailstableView.dataSource = self;
    BettingDetailstableView.delegate = self;
    BettingDetailstableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:BettingDetailstableView];
    
    BettingDetailstableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
    BettingDetailstableView.tableHeaderView.backgroundColor = [UIColor whiteColor];
    // 投注图片
    UIImageView *betPicturesImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 50, 50)];
    [betPicturesImageView setImage:[UIImage imageNamed:@"logo_qtc_2_lg"]];
    [BettingDetailstableView.tableHeaderView addSubview:betPicturesImageView];
    
    // 投注标题
    UILabel *bettingTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(betPicturesImageView.frame)+10, 10, UISCREEN_WIDTH-120, 20)];
    bettingTitleLabel.font = [UIFont systemFontOfSize:16];
    bettingTitleLabel.textColor = [UIColor redColor];
    bettingTitleLabel.text = [NSString stringWithFormat:@"%@",self.G_N_Model.G_N_Title];
    [BettingDetailstableView.tableHeaderView addSubview:bettingTitleLabel];
    
    // 投注时间
    UILabel *bettingSubtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(betPicturesImageView.frame)+10, 30, UISCREEN_WIDTH-120, 20)];
    bettingSubtitleLabel.font = [UIFont systemFontOfSize:12];
    bettingSubtitleLabel.textColor = [UIColor lightGrayColor];
    bettingSubtitleLabel.text = self.G_P_Model.G_P_Name;
    [BettingDetailstableView.tableHeaderView addSubview:bettingSubtitleLabel];
    
    // 投注钱数
    UILabel *bettingTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(betPicturesImageView.frame)+10, CGRectGetMaxY(bettingSubtitleLabel.frame), UISCREEN_WIDTH-120, 40)];
    bettingTimeLabel.font = [UIFont systemFontOfSize:12];
    bettingTimeLabel.textColor = [UIColor lightGrayColor];
    bettingTimeLabel.numberOfLines = 0;
    bettingTimeLabel.text = [NSString stringWithFormat:@"%@\n%@",self.bettingRecordModel.B_L_Issue,self.bettingRecordModel.B_L_AddTime];
    [BettingDetailstableView.tableHeaderView addSubview:bettingTimeLabel];

}

//设置表视图中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return _bettingDetailstitles1.count;
    }else{
        return 1;
    }
}

//设置表视图中的section（分组）数,默认为1
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ([self.bettingRecordModel.B_L_State isEqualToString:@"1"]) {
        return 3;
    }
    return 2;
}

// 设置header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 20;
    }else{
        return 40;
    }


}

- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 35;
}



//设置单元格中的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdent = @"Cell";
    BettingDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (cell == nil) {
        cell = [[BettingDetailsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = [UIFont systemFontOfSize:14];



    }
    NSString *string = [NSString stringWithFormat:@"%@ %@",self.bettingDetailstitles1[indexPath.row],self.bettingDetailstitles2[indexPath.row]];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string];
    [str addAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]} range:NSMakeRange([self.bettingDetailstitles1[indexPath.row] length]+1, [self.bettingDetailstitles2[indexPath.row] length])];
//    [cell.bettingInformationLabel setText:self.bettingDetailstitles2[indexPath.row]];

    //设置数据
    if (indexPath.section == 0) {
        cell.textLabel.attributedText = str;
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
        cell.textLabel.textColor = [UIColor blackColor];
    }else if (indexPath.section == 1){
        cell.textLabel.text = @"继续采购";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor colorWithRed:48/255.0f green:173/255.0f blue:224/255.0f alpha:1];
    }
    else{
        cell.textLabel.text = @"撤单";
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor colorWithRed:48/255.0f green:173/255.0f blue:224/255.0f alpha:1];
    }
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    if (indexPath.section == 2) {
        [self obtainData];
    }
}
-(void)obtainData1
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";
    
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"Game" forKey:@"Model"];
    [parametersDic setObject:@"GetOrderInfo" forKey:@"Action"];
    [parametersDic setObject:self.bettingRecordModel.B_L_Order forKey:@"Oid"];
    [parametersDic setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"123"] forKey:@"Uid"];
   
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",dic);
        [self.bettingDetailstitles2 setObject:dic[@"Code"] atIndexedSubscript:12];
        [self.bettingDetailstitles2 setObject:dic[@"B_L_Code"] atIndexedSubscript:13];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}
-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";
    
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"Game" forKey:@"Model"];
    [parametersDic setObject:@"Withdrawals" forKey:@"Action"];
    [parametersDic setObject:self.bettingRecordModel.B_L_Order forKey:@"B_L_Order"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示" message:[dic objectForKey:@"msg"]preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [alerView addAction:action];
        [self presentViewController:alerView animated:YES completion:nil];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    }];
}

@end
